package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallCouponUser;
import org.linlinjava.litemall.db.mapper.LitemallCouponUserDao;
import org.linlinjava.litemall.db.util.CouponUserConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallCouponUserService {
    @Resource
    private LitemallCouponUserDao couponUserDao;

    public Integer countCoupon(Integer couponId) {
    	LitemallCouponUser entity = new LitemallCouponUser();
    	entity.setCouponId(couponId);
    	entity.setDeleted(false);
    	
    	Long count = couponUserDao.templateCount(entity);
    	return count.intValue();
		/*
		 * LitemallCouponUserExample example = new LitemallCouponUserExample();
		 * example.or().andCouponIdEqualTo(couponId).andDeletedEqualTo(false); return
		 * (int)couponUserMapper.countByExample(example);
		 */
    }

    public Integer countUserAndCoupon(Integer userId, Integer couponId) {
    	
    	LitemallCouponUser entity = new LitemallCouponUser();
    	entity.setCouponId(couponId);
    	entity.setUserId(userId);
    	entity.setDeleted(false);
    	Long count = couponUserDao.templateCount(entity);
    	return count.intValue();
		/*
		 * LitemallCouponUserExample example = new LitemallCouponUserExample();
		 * example.or().andUserIdEqualTo(userId).andCouponIdEqualTo(couponId).
		 * andDeletedEqualTo(false); return
		 * (int)couponUserMapper.countByExample(example);
		 */
    }

    public void add(LitemallCouponUser couponUser) {
        couponUser.setAddTime(LocalDateTime.now());
        couponUser.setUpdateTime(LocalDateTime.now());
        couponUserDao.insertTemplate(couponUser,true);
        //couponUserMapper.insertSelective(couponUser);
    }

    public List<LitemallCouponUser> queryList(Integer userId, Integer couponId, Short status, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallCouponUser>  query = couponUserDao.createLambdaQuery();
    	 if (userId != null) {
    		 query.andEq(LitemallCouponUser::getUserId, userId);
         }
         if(couponId != null){
        	 query.andEq(LitemallCouponUser::getCouponId, couponId);
         }
         if (status != null) {
        	 query.andEq(LitemallCouponUser::getStatus, status);
         }
         query.andEq(LitemallCouponUser::getDeleted, false);

         if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	 query.orderBy(sort + " " + order);
         }

         if (!StringUtils.isEmpty(page) && !StringUtils.isEmpty(size)) {
        	 query.limit(page, size);
         }
         List<LitemallCouponUser> list = query.select()	;
         return list;
    	
			/*
			 * LitemallCouponUserExample example = new LitemallCouponUserExample();
			 * LitemallCouponUserExample.Criteria criteria = example.createCriteria(); if
			 * (userId != null) { criteria.andUserIdEqualTo(userId); } if(couponId != null){
			 * criteria.andCouponIdEqualTo(couponId); } if (status != null) {
			 * criteria.andStatusEqualTo(status); } criteria.andDeletedEqualTo(false);
			 * 
			 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			 * example.setOrderByClause(sort + " " + order); }
			 * 
			 * if (!StringUtils.isEmpty(page) && !StringUtils.isEmpty(size)) {
			 * PageHelper.startPage(page, size); }
			 * 
			 * return couponUserMapper.selectByExample(example);
			 */
    }

    public List<LitemallCouponUser> queryAll(Integer userId, Integer couponId) {
        return queryList(userId, couponId, CouponUserConstant.STATUS_USABLE, null, null, "add_time", "desc");
    }

    public List<LitemallCouponUser> queryAll(Integer userId) {
        return queryList(userId, null, CouponUserConstant.STATUS_USABLE, null, null, "add_time", "desc");
    }

    public LitemallCouponUser queryOne(Integer userId, Integer couponId) {
        List<LitemallCouponUser> couponUserList = queryList(userId, couponId, CouponUserConstant.STATUS_USABLE, 1, 1, "add_time", "desc");
        if(couponUserList.size() == 0){
            return null;
        }
        return couponUserList.get(0);
    }

    public LitemallCouponUser findById(Integer id) {
    	return couponUserDao.single(id);
        //return couponUserMapper.selectByPrimaryKey(id);
    }


    public int update(LitemallCouponUser couponUser) {
        couponUser.setUpdateTime(LocalDateTime.now());
        return couponUserDao.updateTemplateById(couponUser);
        //return couponUserMapper.updateByPrimaryKeySelective(couponUser);
    }

    public List<LitemallCouponUser> queryExpired() {
    	
    	LambdaQuery<LitemallCouponUser>  query = couponUserDao.createLambdaQuery();
    	query.andEq(LitemallCouponUser::getStatus, CouponUserConstant.STATUS_USABLE);
    	query.andLessEq(LitemallCouponUser::getEndTime, LocalDateTime.now());
    	query.andEq(LitemallCouponUser::getDeleted, false);
    	
    	List<LitemallCouponUser> list = query.select();
    	return list;
    	//entity.setStatus(CouponUserConstant.STATUS_USABLE);
    	
		/*
		 * LitemallCouponUserExample example = new LitemallCouponUserExample();
		 * example.or().andStatusEqualTo(CouponUserConstant.STATUS_USABLE).
		 * andEndTimeLessThan(LocalDateTime.now()).andDeletedEqualTo(false); return
		 * couponUserMapper.selectByExample(example);
		 */
    }

    public List<LitemallCouponUser> findByOid(Integer orderId) {
    	
    	LitemallCouponUser entity = new LitemallCouponUser();
    	entity.setOrderId(orderId);
    	entity.setDeleted(false);
    	return couponUserDao.template(entity);
		/*
		 * LitemallCouponUserExample example = new LitemallCouponUserExample();
		 * example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false); return
		 * couponUserMapper.selectByExample(example);
		 */
    }
}
